package day12;

import java.util.Objects;

//本类是用来测试Object类的演示demo
public class TestObject {
    public static void main(String[] args) {
        Student s = new Student();
        Student s1 = new Student("海绵宝宝", 3);
        Student s2 = new Student("海绵宝宝", 3);
        System.out.println(s.hashCode());
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
        System.out.println(s.toString());
        System.out.println(s1.toString());
        System.out.println(s2.toString());
        System.out.println(s1.equals(s2));
        System.out.println(s.equals(s1));
    }
}

class Student {
    String name;
    int age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Student() {
        System.out.println("无参构造方法");

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    public int hashCode() {
        return Objects.hash(name, age);
    }

    public Student(String name, int age) {
        System.out.println("有参构造方法");
        this.name = name;
        this.age = age;
    }

}